package com.qf.spring.anno.service.impl;


import com.qf.spring.anno.dao.UserMapper;
import com.qf.spring.anno.entity.User;
import com.qf.spring.anno.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author nk
 * @date 2022/4/27 21:12
 */
@Service("userService")
//@Scope("prototype")多例的BEAN，默认使用单例的，不需要添加
@Transactional //不写就全是默认的
public class UserServiceImpl implements UserService {
    //按类型自动装配
    @Autowired
//    @Qualifier("userMapper")//按名称分配
//    @Resource  //优先按名称属性名一样的bean装配，如果找不到对应的名字的bean，再按类型装配
    private UserMapper userMapper;

    public UserMapper getUserMapper() {
        return userMapper;
    }

    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true,rollbackFor = Exception.class)
    //方法上标注的事务注解优先于类上的标注
    public User login(String name, String password) {
        User user = userMapper.selectByName(name);
        if (Objects.nonNull(user)){
            if (Objects.equals(user.getPassword(),password)){
                return user;
            }
        }
        return null;
    }

    @Override
    public List<User> selectAll() {
        return userMapper.selectAll();
    }

    @Override
    public int addUser(User user) {
        return userMapper.add(user);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.update(user);
    }

    @Override
    public int deleteUser(Integer id) {
        return userMapper.delete(id);
    }


}
